package com.example.lewisgao.myapplication.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.example.lewisgao.myapplication.R;
import com.example.lewisgao.myapplication.core.MyCookieStore;
import com.example.lewisgao.myapplication.model.AjaxResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.rey.material.widget.EditText;

import org.apache.http.impl.client.DefaultHttpClient;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

/**
 * Created by LewisGao on 2015-06-30 0030.
 */
public class LoginActivity extends Activity {

    @ViewInject(R.id.usernameEditText)
    private EditText username;

    @ViewInject(R.id.passwordEditText)
    private EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ViewUtils.inject(this);
    }

    @OnClick(R.id.loginBtn)
    public void login(View view) {
//        RequestParams params = new RequestParams();
//        params.addBodyParameter("username", username.getText().toString());
//        params.addBodyParameter("password", getMd5(password.getText().toString()));
//        final HttpUtils http = new HttpUtils();
//        http.send(HttpRequest.HttpMethod.POST, "http://10.0.3.2:8080/lamb/login.json", params, new RequestCallBack<String>() {
//            @Override
//            public void onSuccess(ResponseInfo<String> responseInfo) {
//                try {
//                    AjaxResponse ajaxResponse = new ObjectMapper().readValue(responseInfo.result, AjaxResponse.class);
//                    if (ajaxResponse.isOk()) {
//                        DefaultHttpClient dh = (DefaultHttpClient) http.getHttpClient();
//                        MyCookieStore.cookieStore = dh.getCookieStore();
//
//                        getUser();
//                    } else if (ajaxResponse.isError()) {
//                        Toast.makeText(LoginActivity.this, "error", Toast.LENGTH_LONG).show();
//                    }
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(HttpException e, String s) {
//                e.printStackTrace();
//                Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();
//            }
//        });
        Intent intent = new Intent(LoginActivity.this,PlanListActivity.class);
        LoginActivity.this.startActivity(intent);
    }

    private void getUser() {
        HttpUtils http = new HttpUtils();
        http.configCookieStore(MyCookieStore.cookieStore);
        http.send(HttpRequest.HttpMethod.GET, "http://10.0.3.2:8080/lamb/security/user/1", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                try {
                    AjaxResponse ajaxResponse = new ObjectMapper().readValue(responseInfo.result, AjaxResponse.class);
                    if (ajaxResponse.isOk()) {
                        Map userMap = (Map) ajaxResponse.getReturnData().get("user");
                        Toast.makeText(LoginActivity.this, "user:" + userMap.get("username").toString(), Toast.LENGTH_LONG).show();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Toast.makeText(LoginActivity.this, s, Toast.LENGTH_LONG).show();

            }
        });

    }

    private String getMd5(String str) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }

        char[] charArray = str.toCharArray();
        byte[] byteArray = new byte[charArray.length];

        for (int i = 0; i < charArray.length; i++) {
            byteArray[i] = (byte) charArray[i];
        }

        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5.digest(byteArray)) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString().toUpperCase();
    }
}
